package com.lkd.handler;

import com.google.common.base.Strings;
import com.lkd.business.MsgHandler;
import com.lkd.common.VMSystem;
import com.lkd.config.TopicConfig;
import com.lkd.contract.TaskCompleteContract;
import com.lkd.emq.Topic;
import com.lkd.service.VendingMachineService;
import com.lkd.utils.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
@Component
@Topic(TopicConfig.VMS_COMPLETED_TOPIC)
public class TaskCompletedMsgHandler implements MsgHandler {

    //自动注入售货机服务
    @Autowired
    VendingMachineService vmService;

    @Override
    public void process(String jsonMsg) throws IOException {
        //1.解析消息为，完成运维工单协议类
        TaskCompleteContract taskCompleteContract = JsonUtil.getByJson(jsonMsg, TaskCompleteContract.class);
        if (taskCompleteContract==null|| Strings.isNullOrEmpty(taskCompleteContract.getInnerCode())) {
            return;
        }
        //2.如果是投放工单，将售货机状态修改为运营状态
        if (taskCompleteContract.getTaskType()== VMSystem.TASK_TYPE_DEPLOY) {
            vmService.updateStatus(taskCompleteContract.getInnerCode(),VMSystem.VM_STATUS_RUNNING);
        }

        //3.如果是撤机工单，将售货机状态修改为撤机状态。
        if (taskCompleteContract.getTaskType()== VMSystem.TASK_TYPE_REVOKE) {
            vmService.updateStatus(taskCompleteContract.getInnerCode(),VMSystem.VM_STATUS_REVOKE);
        }
    }
}
